package edu.fdzc.learn.controller;

import edu.fdzc.learn.common.Result;
import edu.fdzc.learn.entity.Lesson;
import edu.fdzc.learn.service.LessonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/lesson")
public class LessonController {

    @Autowired
    private LessonService lessonService;

    @PostMapping("/add")
    public Result addLesson(@RequestBody Lesson lesson) {
        lessonService.add(lesson);
        return Result.success();
    }

    @PutMapping("/update")
    public Result updateLesson(@RequestBody Lesson lesson) {
        lessonService.update(lesson);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteLesson(@PathVariable int id) {
        lessonService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Lesson> getLessonById(@PathVariable int id) {
        Lesson lesson = lessonService.findById(id);
        return Result.success(lesson);
    }

    @GetMapping
    public Result<List<Lesson>> getAllLesson() {
        List<Lesson> list = lessonService.getall();
        return Result.success(list);
    }


    @GetMapping("course/{courseId}")
    public Result<List<Lesson>> findByCourseId(@PathVariable int courseId) {
        List<Lesson> list = lessonService.findByCourseId(courseId);
        return Result.success(list);
    }



}
